package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.QuotationDao;
import cn.gson.boot.model.dao.QuotationdetailsDao;
import cn.gson.boot.model.dao.SalesDao;
import cn.gson.boot.model.dao.ShenHeDao;
import cn.gson.boot.model.mapper.QuotationDetailsMapper;
import cn.gson.boot.model.mapper.QuotationMapper;
import cn.gson.boot.model.mapper.SalesDetailsMapper;
import cn.gson.boot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class QuotationService {
    @Autowired
    QuotationMapper quotationMapper;
    @Autowired
    QuotationDao quotationDao;
    @Autowired
    SalesDetailsMapper salesDetailsMapper;
    @Autowired
    QuotationdetailsDao quotationdetailsDao;
    @Autowired
    QuotationDetailsMapper quotationDetailsMapper;
    @Autowired
    SalesDao salesDao;
    @Autowired
    ShenHeDao shenHeDao;

    //查询报价列表
    public List<Quotation> selectQuotation(String search,String empName){
        return quotationMapper.selectQuotation(search,empName);
    }

    //新增报价单
    public void addQuotation(List detailss, Quotation quotation) {
        if (quotation.getQuotationId() == 0) {
            JSONArray json = JSONArray.parseArray(String.valueOf(detailss));
            String Nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());//一个string类型的当前时间
            java.sql.Date mysqldate = java.sql.Date.valueOf(Nowtime);
            quotation.setQuotationUpdatetime(mysqldate);
            quotation.setQuotationApproval("待审核");
            quotation.setQuotationSc(0);
            quotationDao.save(quotation);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(quotation.getQuotationId());
            shenhe.setShShouming("报价单");
            shenhe.setShZhuangtai("待审核");
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setEmp(null);
            shenhe.setShenqingPeople(quotation.getQuotationMan());
            shenHeDao.save(shenhe);
            Sales sales = salesDao.findById(quotation.getSales().getSalesId()).get();
            Stage stage = new Stage();
            stage.setStageId(4);
            sales.setStage(stage);
            salesDao.save(sales);
            quotationdetailsDao.deleteAll(quotationDetailsMapper.selectQuotationDetailss(quotation.getQuotationId()));
            Quotationdetails quotationdetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer quotationdId = ob.getInteger("quotationdId");
                Integer productId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(productId);
                quotationdetails = new Quotationdetails();
                if (quotationdId != null) {
                    quotationdetails.setQuotationdId(quotationdId);
                }
                quotationdetails.setQuotationdNum(purNumber);
                quotationdetails.setQuotationdPrice(purPrice);
                quotationdetails.setQuotationdMoney(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                quotationdetails.setQuotation(quotation);
                quotationdetails.setQuotationdMan(quotation.getQuotationMan());
                quotationdetails.setProductt(productt);
                quotationdetails.setQuotationdDate(mysqldate);
                quotationdetailsDao.save(quotationdetails);
            }
        }else{
            JSONArray json = JSONArray.parseArray(String.valueOf(detailss));
            quotationDao.save(quotation);
            Sales sales = salesDao.findById(quotation.getSales().getSalesId()).get();
            Stage stage = new Stage();
            stage.setStageId(4);
            sales.setStage(stage);
            salesDao.save(sales);
            quotationdetailsDao.deleteAll(quotationDetailsMapper.selectQuotationDetailss(quotation.getQuotationId()));
            Quotationdetails quotationdetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer quotationdId = ob.getInteger("quotationdId");
                Integer productId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(productId);
                quotationdetails = new Quotationdetails();
                if (quotationdId != null) {
                    quotationdetails.setQuotationdId(quotationdId);
                }
                quotationdetails.setQuotationdNum(purNumber);
                quotationdetails.setQuotationdPrice(purPrice);
                quotationdetails.setQuotationdMoney(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                quotationdetails.setQuotation(quotation);
                quotationdetails.setQuotationdMan(quotation.getQuotationMan());
                quotationdetails.setProductt(productt);
                quotationdetailsDao.save(quotationdetails);
            }
        }
    }




    //删除报价单
    public void delQuotation(Integer quotationId) {
        Quotation quotation = quotationDao.findById(quotationId).get();
        quotation.setQuotationSc(1);
        quotationDao.save(quotation);
    }

    //修改报价单
    public List<Quotation> upQuotation(Integer quotationId) {
        return quotationMapper.selectQuotationId(quotationId);
    }

    //查询需要修改的报价详情
    public List<Quotationdetails> quotationDetailsList(Integer quotationId) {
        return quotationDetailsMapper.selectQuotationDetailss(quotationId);
    }

    //查询单号
    public String selectOdd() {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date d = new Date();
        String code = quotationMapper.selectOdd();
        if(null==code || "".equals(code)) {
            code = "BJ"+sdf.format(d)+"1";
        }else {
            int zhi = Integer.parseInt(code)+1;
            code = "BJ"+sdf.format(d)+ zhi +"";
        }
        return code;
    }


    //报价单客户列表
    public List<Kehu> selectKh(String khsearch,String empName) {
        return quotationMapper.selectKehu(khsearch,empName);
    }

    //客户联系人列表
    public List<Contacts> selectLxr(String khName) {
        return quotationMapper.selectLxr(khName);
    }

    //对应机会列表
    public List<Sales> selectSales(String khName) {
        return quotationMapper.selectSales(khName);
    }

    //查询需要报价的产品
    public List<Salesdetails> selectProductList(Integer salessId) {
        return salesDetailsMapper.Salesxq(salessId);
    }

    //查询已签合同的报价单不能删除
    public Quotation selectHt(Integer quotationId) {
        return quotationMapper.selectHt(quotationId);
    }

    //查询需要打印的报价单
    public Quotation selectContacts(Integer quotationId, String name) {
        return quotationMapper.selectContacts(quotationId,name);
    }

    //销售机会页面已生成报价单不可删除
    public List<Quotation> selectSalesQuotation(Integer salesId) {
        return quotationMapper.selectSalesQuotation(salesId);
    }


}
